using System;
using System.Collections.Generic;
using System.Text;

namespace TmxUtil
{
    [Serializable]
    public class ResXStrings
    {
        private List<string> _keys = new List<string>();
        Dictionary<string, string> _byKey = new Dictionary<string, string>();
        Dictionary<string, string> _refByKey = new Dictionary<string, string>();

        public int Count
        {
            get { return _keys.Count;  }
        }
        
        public IEnumerable<string> Keys
        {
            get
            {
                return _keys;
            }
        }

        public string this[string key]
        {
            get
            {
                string ret;
                _byKey.TryGetValue(key, out ret);
                return ret;
            }
        }

        public string GetReference(string key)
        {
            string ret;
            _refByKey.TryGetValue(key, out ret);
            return ret;
        }

        public void Set(string key, string value)
        {
            if (!_byKey.ContainsKey(key))
                _keys.Add(key);

            _byKey[key] = value;
        }

        public void Add(string key, string value, string reference)
        {
            Set(key, value);
            _refByKey[key] = reference;
        }

        public static string GetDigest(string fullValue)
        {
            StringBuilder ret = new StringBuilder();
            int n = 0;
            foreach(char c in fullValue)
            {
                if (n >= 88)
                    break;

                if (((int)c) >= 32)
                    ret.Append(c);

                else 
                    ret.Append(' ');

                n++;
                
            }

            if (fullValue.Length > ret.Length)
                ret.Append("...");

            return ret.ToString();
        }
    }
}
